import CustomHook from './miniprogram_npm/spa-custom-hooks/index';
import { currentAccount, loginWxampForWechatCallback } from "./service/api/api-login";
import access from './utils/access';
// app.ts
App<IAppOption>({
  globalData: {
    currentUser: undefined,
    access: undefined,
    satoken: "",
    navBarHeight: 0,//导航栏高度
    menuRight: 0,//胶囊距离右边的宽度
    PageActive: true
  },
  //防抖
  preventActive(fn) {
    const self = this
    if (this.globalData.PageActive) {
      this.globalData.PageActive = false
      if (fn) fn()
      setTimeout(() => {
        self.globalData.PageActive = true
      }, 1500); //设置该时间内重复触发只执行第一次，单位ms，按实际设置
    } else {
      console.log('重复点击或触发')
    }
  },
  onLaunch() {
    //获取自定义navigationBar高度
    const that = this;
    // 获取系统信息
    const systemInfo = wx.getSystemInfoSync();

    // 胶囊按钮位置信息
    const menuButtonInfo = wx.getMenuButtonBoundingClientRect();

    if (systemInfo.model.toLowerCase().includes('ipad')) {
      // 导航栏高度 = 状态栏高度 + 44
      that.globalData.navBarHeight = systemInfo.statusBarHeight + 0;
    } else {
      // 导航栏高度 = 状态栏高度 + 44
      that.globalData.navBarHeight = systemInfo.statusBarHeight + 44;
    }
    that.globalData.menuRight = systemInfo.screenWidth - menuButtonInfo.right + 87;

    CustomHook.install({
      'CurrentUser': {
        name: 'CurrentUser',
        watchKey: 'currentUser',
        onUpdate(val: any) {
          return !!val;
        }
      },
    }, this.globalData || 'globalData')
    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        loginWxampForWechatCallback({ serviceName: "xtbs", code: res.code }).then(response => {

          if (response.code === 0) {
            let app = getApp();

            const { state, satoken, accountId } = response.data;

            /**
             * state 状态
             * 1、无账号
             * 2、有账号，未激活
             * 3、有账号、有激活、未启用
             * 4、有账号、有激活、已启用
             * 5、有账户已激活已启用多用户首次登录
             */
            app.globalData.satoken = satoken;

            if (state === 1) {
              //无账号
              //跳转到注册页面，注册用户，需要输入手机号、姓名、组织
              wx.reLaunch({
                url: '/pages/user/bind/index'
              })
            } else if (state === 2 || state === 3) {
              //2:有账号，未激活
              //3:有账号、有激活、未启用
              wx.reLaunch({
                url: `/pages/user/state/index?state=${state}`
              })
            } else if (state === 4) {
              //有账号、有激活、已启用
              //正常跳转到主界面
              //正常登录
              currentAccount().then(responseCurrentUser => {
                if (responseCurrentUser.code == 0) {
                  app.globalData.access = access(responseCurrentUser.data);
                  app.globalData.currentUser = responseCurrentUser.data;
                  wx.setStorageSync('currentUser', responseCurrentUser.data);// 存到缓存
                  console.log('============================');
                  // wx.reLaunch({
                  //   url: `/pages/home/index`
                  // })
                }
              });

            } else if(state === 5) {
              //有账户已激活已启用多用户首次登录
              //选择登录
              wx.reLaunch({
                url: `/pages/user/select/index?accountId=${accountId}`
              })
              //跳转到选择登录组织页面
            }
          }
        })
      },
    })
  },
  onShow() {

  }
})